package edu.virginia.cs4501.team22.objects;


public class Event implements Comparable<Event> {
	private String buildingName;
	private String identifier; //Readable title; user defined

	private String roomNumber;
	private int floor;
	private char wing;


	public Event(String id, String building, String room) {
		//full constructor, make new ones as you see fit (cascading constructors only...)
		this.identifier = id;
		this.buildingName = building;

		if (room.length()>1) {
			switch (room.charAt(0)) {
			case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':
				wing = room.charAt(0);
				room = room.substring(room.charAt(1));
				break;
			default:
				wing = '_';
				break;
			}
			try {
				floor = Character.getNumericValue((room.charAt(0)));
			} catch (Throwable t) {
				floor = 0;
			}

			this.roomNumber = room;
		}
	}
	public Event(String building, String room) {
		this("", building, room);
	}

	/**
	 * @Override Object
	 */
	@Override
	public Object clone() {
		return new Event(
				this.identifier,
				this.buildingName,
				this.roomNumber);
	}

	/**
	 * @Override Comparable
	 * @param another (Class)Object to be compared to this Class
	 * @return int: before is negative,
	 */
	@Override
	public int compareTo(Event another) {
		return (this.buildingName.compareTo(another.buildingName) +
				this.roomNumber.compareTo(another.roomNumber) );
	}

	/**
	 * @Override Object
	 * @param another (Class)Object to be compared to this Class
	 * @return boolean true if equal, false otherwise
	 */
	@Override
	public boolean equals(Object another) {
		if (!(another instanceof Event))
			return false;
		Event comp = (Event)another;
		//optimization
		if (this == comp) {
			return true;
		}
		return ((this.buildingName.equals(comp.buildingName)) &&
				(this.roomNumber.equals(comp.roomNumber)) );
	}

	/**
	 * @return the building name this class is located at
	 */
	public String getBuildingName() {
		return buildingName;
	}

	public int getFloor() {
		return floor;
	}

	/**
	 * @return the user-defined identifier
	 */
	public String getIdentifier() {
		return identifier;
	}

	/**
	 * @return the room number the class is associated with
	 */
	public String getRoomNumber() {
		return roomNumber;
	}

	public char getWing() {
		return wing;
	}

	/**
	 * @param another the Class to be compared to this to determine overlapping time conflicts.
	 * @return returns number of seconds of conflict time. return<0 number of seconds of overlap; return>=0 number of seconds between
	 *
	public int hasTimeConflict(Event another) {
		final int myStart = this.startTime.hour*60^2 + this.startTime.minute*60 + this.startTime.second;
		final int myEnd = this.endTime.hour*60^2 + this.endTime.minute*60 + this.endTime.second;
		final int anotherStart = another.startTime.hour*60^2 + another.startTime.minute*60 + another.startTime.second;
		final int anotherEnd = another.endTime.hour*60^2 + another.endTime.minute*60 + another.endTime.second;
		int greaterStart, lesserEnd; //kinds self explanatory, pick the greater start time and the lesser end time

		//initializes the greater of the two start times and the lesser of the two ending times
		if (myStart >= anotherStart) {greaterStart = myStart;}
		else {greaterStart = anotherStart;}
		if (myEnd <= anotherEnd) {lesserEnd = myEnd;}
		else {lesserEnd = anotherEnd;}

		//this checks for non-overlapping class times, and calculates the time in-between
		if (myStart > anotherEnd)
			return myStart-anotherEnd;
		else if (myEnd < anotherStart)
			return anotherStart-myEnd;
		//the real work is here, actually calculating time of over lap
		else if (myEnd < anotherEnd)
			return -1*(myEnd - greaterStart);
		else if (myStart > anotherStart)
			return -1*(lesserEnd - myStart);
		else
			return -1*(anotherEnd - anotherStart);

		//if you don't understand this, draw line diagrams representing times and run through the logic.
	}
	 */

	/**
	 * @param buildingName the name of the building to set
	 */
	public void setBuildingName(String buildingName) {
		this.buildingName = buildingName;
	}

	/**
	 * @param identifier the user defined name to set
	 */
	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	/**
	 * @param roomNumber the roomNumber to set
	 */
	public void setRoomNumber(String roomNumber) {
		this.roomNumber = roomNumber;
	}

	/**
	 * @Override Object
	 * @return String String representation of a team22.objects.Class object
	 */
	@Override
	public String toString() {
		StringBuilder build = new StringBuilder();
		build.append(this.identifier);
		build.append(": Room[");
		build.append(this.buildingName);
		build.append("- ");
		build.append(this.roomNumber);
		build.append("]");
		return build.toString();
	}
}
